package Controller;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.PreparedStatement;

import Service.UserService;

@Controller
public class UserController {
	
	@Resource(name = "userService")
	private UserService UserService;

	public UserService getUserService() {
		return UserService;
	}

	public void setUserService(UserService userService) {
		UserService = userService;
	}
	
	@RequestMapping("/wendy")
    @ResponseBody  
	public String addUser(){
		//int flag = UserService.add();
		System.out.println("I am coming in...");
		return "wendy&watson ";
		
	}
	
	@RequestMapping("/danel")
    @ResponseBody  
	public String addDate(String name,String content,String top,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html;charset=utf-8");
		
		name = new String(name.getBytes("ISO-8859-1"), "utf-8");
		content = new String(content.getBytes("ISO-8859-1"), "utf-8");
		System.out.println(name);
		System.out.println(content);
		System.out.println(top);
		
		int flag = UserService.add(name,content,top);
	    System.out.println("插入数据成功？："+flag);
		return flag+"";
		
	}
	
	@RequestMapping("/spark")
    @ResponseBody  
	public String showDate(String name,String content,String top,String page,String rows,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html;charset=utf-8");
		
		//name = new String(name.getBytes("ISO-8859-1"), "utf-8");
		//content = new String(content.getBytes("ISO-8859-1"), "utf-8");
		
		System.out.println("name "+name);
		System.out.println("content "+content);
		System.out.println("top "+top);
		System.out.println("page "+page);
		System.out.println("rows "+rows);
		
		String jsonlist = UserService.show(name, content, top, page, rows);
		
		//jsonlist = new String(name.getBytes("utf-8"), "ISO-8859-1"); 
		//jsonlist = new String(content.getBytes("ISO-8859-1"), "utf-8");
		
	    System.out.println("读取数据成功？："+jsonlist);
	 
		return jsonlist;
		
	}
    
	@RequestMapping("/check") 
	@ResponseBody 
	public String checkDate(String name,String content,String top,String page,String rows,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html;charset=utf-8");
		
		System.out.println("name "+name);
		System.out.println("content "+content);
		System.out.println("top "+top);
		System.out.println("page "+page);
		System.out.println("rows "+rows);
		
		String jsonlist = UserService.show(name, content, top, page, rows);
		
	    System.out.println("读取数据成功？："+jsonlist);
		return jsonlist;
		
		
	}
	
	@RequestMapping("/destroy") 
	@ResponseBody 
	public String checkDate(String name) throws UnsupportedEncodingException{
		name = new String(name.getBytes("ISO-8859-1"), "utf-8");
		
		System.out.println("name "+name);
	
		
		int flag = UserService.destroy(name);
		
	    System.out.println("删除数据成功？："+flag);
		return flag+"";
		
		
	}
	
	@RequestMapping("/gain") 
	@ResponseBody 
	public String gainDate(String name,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html;charset=utf-8");
		
		System.out.println("name "+name);
		
		
		String jsonlist = UserService.gain(name);
		
	    System.out.println("获取content和name数据成功？："+jsonlist);
		return jsonlist;
		
		
	}
	
	@RequestMapping("/edit") 
	@ResponseBody 
	public String editDate(String name,String content,String top,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html;charset=utf-8");
		
		name = new String(name.getBytes("ISO-8859-1"), "utf-8");
		content = new String(content.getBytes("ISO-8859-1"), "utf-8");
		System.out.println(name);
		System.out.println(content);
		System.out.println(top);
	
		
		
		int flag = UserService.edit(name,content,top);
		
	    System.out.println("修改数据成功？："+flag);
		return flag+"";
		
		
	}


}
